home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / sound / dsond131.zip / PLAYMONO.C < prev    next >
C/C++ Source or Header  |  1994-03-06  |  4KB  |  183 lines

  1.  
  2. /*************************************************************************/
  3. /*                   PlayMono.c                 */
  4. /*           Contains code used to play mono samples.          */
  5. /*************************************************************************/
  6.  
  7. #include <exec/types.h>
  8. #include <exec/exec.h>
  9. #include <devices/audio.h>
  10. #include <dos/dos.h>
  11. #include <intuition/intuition.h>
  12. #include <intuition/intuitionbase.h>
  13. #include <graphics/gfxbase.h>
  14. #include <stdlib.h>
  15.  
  16. #include "dsound.h"
  17.  
  18. #include <proto/intuition.h>
  19. #include <proto/exec.h>
  20. #include <proto/dos.h>
  21.  
  22. UBYTE rightAMap[]={4,2,1,8};
  23. UBYTE leftAMap[]={1,8,2,4};
  24. UBYTE eitherAMap[]={1,2,4,8};
  25. UBYTE bothAMap[]={8,3,5,10};
  26.  
  27. extern UBYTE volume;
  28. extern UWORD speed;
  29. extern ULONG bufSize;
  30.  
  31. extern BOOL readAll;
  32. extern BOOL loop;
  33. extern struct Window *window;
  34.  
  35. extern ULONG signalMask;
  36.  
  37. /*Play a sample out of one speaker (left, right, or either)*/
  38. void playMonoSample(BPTR file,channel audioChannel,struct Voice8Header *vhdr,
  39.             ULONG len)
  40. {
  41.    struct IOAudio *iob1,*iob2,*curIob,*altIob;
  42.    ULONG toRead;
  43.    ULONG sampleSize=len;
  44.    ULONG amountLeft;
  45.    BOOL done=FALSE;
  46.    UBYTE *allocationMap;
  47.  
  48.    /*Load the entire sample into memory, if the user so specified*/
  49.    if(readAll)
  50.    {
  51.       storeLeft(file,len,bufSize);
  52.       file=0L;
  53.    }
  54.  
  55.    /*Decide which audio channel will be allocated*/
  56.    switch(audioChannel)
  57.    {
  58.       case MONO_LEFT:
  59.      allocationMap=leftAMap;
  60.      break;
  61.       case MONO_RIGHT:
  62.      allocationMap=rightAMap;
  63.      break;
  64.       case UNSPECIFIED:
  65.      allocationMap=eitherAMap;
  66.      break;
  67.    }
  68.  
  69.    /*Get the first audio channel*/
  70.    iob1=GetAudioChannel(bufSize,allocationMap);
  71.    if(iob1==NULL)
  72.    {
  73.       WriteMsg("Couldn't create the first buffer\n");
  74.       cleanup(150);
  75.    }
  76.  
  77.    /* If the user didn't specify a volume, get it from the VHDR */
  78.    if(volume==0)
  79.       volume=(vhdr->volume>>10);
  80.  
  81.    /* If the VHDR gave a volume of zero, use maximum volume*/
  82.    if(volume==0)
  83.       volume=64;
  84.  
  85.    /* Get the samples/sec rate (either the rate given by the user, or the*/
  86.    /* rate found in the VHDR) */
  87.    if(speed==0)
  88.       speed=1000000000/(vhdr->samplesPerSec*279);
  89.    else
  90.       speed=1000000000/(speed*279);
  91.  
  92.    InitAudioChannel(iob1,volume,speed);
  93.  
  94.    /*Get the 2nd audio channel*/
  95.    iob2=DuplicateAudioChannel(iob1);
  96.  
  97.    if(iob2==NULL)
  98.    {
  99.       FreeAudioChannel(iob1);
  100.       WriteMsg("Couldn't create the second buffer\n");
  101.       cleanup(175);
  102.    }
  103.  
  104.    /* Load the first buffer*/
  105.    toRead=MIN(len,bufSize);
  106.    LoadAudioBuffer(file,iob1,toRead);
  107.  
  108.    len-=toRead;
  109.    if(len==0 && loop)
  110.    {
  111.       len=sampleSize;
  112.       if(!readAll)
  113.      Seek(file,-sampleSize,OFFSET_CURRENT);
  114.    }
  115.  
  116.    /*Store the number of samples to be played*/
  117.    iob1->ioa_Length=toRead;
  118.  
  119.    updateSampleInfo(0,sampleSize,vhdr->samplesPerSec);
  120.  
  121.    /*Queue up the play request*/
  122.    BeginIO((struct IORequest *)iob1);
  123.  
  124.    curIob=iob2;
  125.    altIob=iob1;
  126.  
  127.    /*Loop while there's stuff to read*/
  128.    while(!done)
  129.    {
  130.       toRead=MIN(len,bufSize);
  131.       if(toRead!=0)
  132.       {
  133.      LoadAudioBuffer(file,curIob,toRead);
  134.      curIob->ioa_Length=toRead;
  135.      BeginIO((struct IORequest *)curIob);
  136.      amountLeft=len-=toRead;
  137.      if(len==0 && loop)
  138.      {
  139.         len=sampleSize;
  140.         if(!readAll)
  141.            Seek(file,-sampleSize,OFFSET_CURRENT);
  142.      }
  143.      done=FALSE;
  144.       }
  145.       else
  146.      done=TRUE;
  147.  
  148.       /*Wait for the buffer to finish*/
  149.       if((Wait((1<<altIob->ioa_Request.io_Message.mn_ReplyPort->mp_SigBit) |
  150.        signalMask) & SIGBREAKF_CTRL_C) == SIGBREAKF_CTRL_C)
  151.      done=TRUE;
  152.  
  153.       updateSampleInfo(sampleSize-amountLeft-toRead,sampleSize,vhdr->samplesPerSec);
  154.       if(window!=NULL && GetMsg(window->UserPort)!=NULL)
  155.       {
  156.      done=TRUE;
  157.       }
  158.  
  159.       swapPointers(&curIob,&altIob);
  160.    }
  161.  
  162.    /*Restore the buffer lengths, so that FreeAudio() channel, etc., knows*/
  163.    /*how much memory to free*/
  164.    iob1->ioa_Length=iob2->ioa_Length=bufSize;
  165.  
  166.    FreeAudioChannel(iob1);
  167.    DeleteDuplication(iob2);
  168.  
  169.    return;
  170. }
  171.  
  172. void swapPointers(struct IOAudio **first,struct IOAudio **second)
  173. {
  174.    void *temp;
  175.  
  176.    temp=*first;
  177.    *first=*second;
  178.    *second=temp;
  179.  
  180.    return;
  181. }
  182. /*End of PlayMono.c*/
  183.